<template>
  <el-card class="w-full pt-[5px]">
    <el-form :model="searchForm" class="mt-[20px]" label-width="110px" ref="searchFormRef">
      <el-row :gutter="20">
        <el-col :span="6">
          <el-form-item label="经费卡号" class="mb-[0px]" prop="feeCardNum">
            <el-input v-model="searchForm.feeCardNum" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称" class="mb-[0px]" prop="projectName">
            <el-input v-model="searchForm.projectName" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="经费负责人" class="mb-[0px]" prop="feeChargerName">
            <el-input v-model="searchForm.feeChargerName" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <div class="flex justify-end">
            <el-button type="primary" @click="searchFormOpen = !searchFormOpen">
              更多
              <el-icon color="#fff">
                <CaretTop v-show="searchFormOpen" />
                <CaretBottom v-show="!searchFormOpen" />
              </el-icon>
            </el-button>
            <el-button type="primary" @click="search">查询</el-button>
            <el-button @click="resetForm">重置</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20" class="mt-[20px]" v-show="searchFormOpen">
        <el-col :span="6">
          <el-form-item label-width="0">
            <el-input v-model="searchForm.params.minfee" placeholder="最小结题金额" clearable
              style="width: calc(50% - 10px)" />
            <span style="width: 20px; text-align: center">至</span>
            <el-input v-model="searchForm.params.maxfee" placeholder="最大结题金额" clearable
              style="width: calc(50% - 10px)" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="结题卡号" prop="transferFeeCode">
            <el-input v-model="searchForm.transferFeeCode" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目编号" prop="projectCode">
            <el-input v-model="searchForm.projectCode" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目分类" prop="projectType">
            <CustomTreeSelect
              paramsName="项目分类（所有-有级联）"  
              v-model="searchForm.projectType"
              valueKey="id"
              labelKey="label"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目性质" prop="projectClass">
            <el-select v-model="searchForm.projectClass" clearable>
              <el-option
                v-for="item in projectClassList"
                :key="item.code"
                :label="item.name"
                :value="item.code"
              />
            </el-select>
          </el-form-item>
        </el-col>                         
        <el-col :span="12">
          <el-form-item label-width="45">
            <CommonDateSelect placeholder="立项日期" v-model:beginTime="searchForm.params.beginTime"
              v-model:endTime="searchForm.params.endTime" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>

  <el-card class="w-full pt-[5px] mt-[10px]">
    <div class="mb-[20px]" v-equalRole="['biz_kycgly']">
      <el-button @click="handleExport"> <svg-icon iconClass="export" class="mr-[5px]"></svg-icon>导出 </el-button>
    </div>
    <el-table :data="tableData" class="w-full" stripe row-key="id" ref="tableRef"
        @sort-change="sortChange">
      <el-table-column prop="feeCardNum" label="经费卡号" sortable="c.fee_card_num">
        <template #default="{ row }">
          <el-link type="primary" @click="handleView(row)">{{ row.feeCardNum }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="projectName" label="项目名称" sortable="p.name">
        <template #default="{ row }">
          <el-link type="primary" @click="handleShow(row.projectId)">{{ row.projectName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="feeChargerName" label="经费负责人" sortable="c.fee_charger_name"/>
      <el-table-column prop="chargerName" label="项目负责人" sortable="p.charger_name"/>
      <!-- <el-table-column prop="budgettotal" label="总预算(万元)">
        <template #default="{ row }">
          <span>{{ FloatDiv(row.budgettotal,10000) }}</span>
        </template>
      </el-table-column> -->
      <el-table-column prop="incometotal" label="总到账(万元)">
        <template #default="{ row }">
          <span>{{ FloatDiv(row.incometotal,10000) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="paytotal" label="结余(万元)">
        <template #default="{ row }">
          <span>{{ FloatDiv(row.paytotal,10000) }}</span>
        </template>
      </el-table-column>
     
      <el-table-column prop="transferState" label="状态" sortable="c.transfer_state">
        <template #default="{ row }">
          <CommonLabel :dataList="stateList" :value="row.transferState" />
        </template>
      </el-table-column>
      <el-table-column prop="transferFee" label="结题金额(元)" sortable="c.transfer_fee">
        <template #default="{ row }">
          <span>{{ FloatDiv(row.transferFee,10000) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="transferFeeCode" label="结题卡号" sortable="c.transfer_fee_code"/>
    </el-table>
    <div class="mt-[20px] flex justify-end">
      <el-pagination v-bind="pagination" @update:current-page="currentChange" @update:page-size="sizeChange" />
    </div>
  </el-card>
  <ViewModal ref="viewRef" />
  <ViewProjectModal ref="viewProjectRef" />
</template>

<script setup>
import useTable from "@/hooks/useTable";
import CommonDateSelect from "@/views/fee/components/CommonDateSelect.vue";
import CommonLabel from "@/views/fee/components/CommonLabel.vue";
import CustomTreeSelect from "@/views/fee/components/CustomTreeSelect.vue";
import ViewModal from "./components/ViewModal.vue";
import ViewProjectModal from "@/views/project/zx/lxxm/components/ViewModal.vue";
import { getList } from "@/api/fee/rz/xmjfk/index.js";
import { getCommonData } from "@/api/project/common.js";
import useCommonFetch from "@/hooks/useCommonFetch";
import { exportFile } from "@/api/fee/common.js";
import { downloadFile } from "@/utils/main";
import { FloatDiv } from "@/utils/pub";
const searchFormOpen = ref(false);
const searchFormRef = ref();
const tableRef = ref();
const viewRef = ref();
const viewProjectRef = ref();
const unitList = useCommonFetch("成果归属单位"); //所有单位; 
const stateList = useCommonFetch("结题或进行状态"); ;//状态
const projectClassList = useCommonFetch("项目性质");//项目性质
const searchForm = ref({
  feeCardNum:"",
  feeChargerName:"",
  chargerName: "",
  projectCode: "", 
  projectClass:"",
  projectName: "", 
  transferFeeCode:"",
  orderByColumn:"",
  isAsc:"",
  projectType:[],
  params:{beginTime: "",endTime: "",maxfee:"",minfee:"",},
});

const { loading, pagination, sizeChange, currentChange, tableData, handleDelete, search } =
useTable((pagin) => 
  getList({
      pageNum: pagin.pageNum,
      pageSize: pagin.pageSize,
      ...searchForm.value,
      projectType: searchForm.value.projectType.join(","),
    })
  );
//点击重置
const resetForm = () => {
  searchFormRef.value.resetFields();
  searchForm.value.params.beginTime = "";
  searchForm.value.params.endTime = "";
  searchForm.value.params.maxfee = "";
  searchForm.value.params.minfee = "";
  search();
};

//排序
const sortChange =(row)=>{
 searchForm.value.orderByColumn = row.column.sortable;
 searchForm.value.isAsc=row.order;
 search();
}

//点击项目名称
const handleShow = (id) => {
  viewProjectRef.value.view(id);
};

//点击经费卡号
const handleView = (row) => {
  viewRef.value.view(row);
};

/** 导出按钮操作 */
const handleExport = () => {
  let params = {...searchForm.value,projectType: searchForm.value.projectType.join(",")};
  exportFile('/feemanage/feecard/export',params).then((res) => {
    downloadFile(res, `项目经费卡_${new Date().getTime()}.xlsx`);
  }).catch((error) => {
    console.log(error);
  }); 
}
</script>

<style scoped lang="scss"></style>